package dfbz.com.controller.view;

import dfbz.com.controller.BaseServlet;
import dfbz.com.model.Article;
import dfbz.com.model.Dept;
import dfbz.com.service.ArticleService;
import dfbz.com.service.DeptService;
import dfbz.com.service.UserService;
import dfbz.com.service.impl.ArticleServiceImpl;
import dfbz.com.service.impl.DeptServiceImpl;
import dfbz.com.service.impl.UserServiceImpl;
import dfbz.com.utils.PageUtil;
import dfbz.com.utils.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;

@WebServlet("/view/*")
public class UIServlet extends BaseServlet {

    private UserService userService = new UserServiceImpl();
    private DeptService deptService = new DeptServiceImpl();
    private ArticleService articleService = new ArticleServiceImpl();

    public void index(HttpServletRequest req, HttpServletResponse resp) {
        try {
            req.getRequestDispatcher("/index.jsp").forward(req, resp);
        } catch (ServletException | IOException e) {
            e.printStackTrace();
        }
    }


    public void home(HttpServletRequest req, HttpServletResponse resp){
        try {
            req.getRequestDispatcher("/html/home.jsp").forward(req, resp);
        } catch (ServletException | IOException e) {
            e.printStackTrace();
        }
    }


    public void showRegister(HttpServletRequest req, HttpServletResponse resp) {
        try {
            req.getRequestDispatcher("/register.jsp").forward(req, resp);
        } catch (ServletException | IOException e) {
            e.printStackTrace();
        }
    }


    public void forget(HttpServletRequest req, HttpServletResponse resp){

        try {
            req.getRequestDispatcher("/forget.jsp").forward(req, resp);
        } catch (ServletException | IOException e) {
            e.printStackTrace();
        }
    }

    public void userList(HttpServletRequest request, HttpServletResponse response) {
        try {
            String currentPage = request.getParameter("page");
            String search = request.getParameter("search");
            if(null != search && !StringUtil.isContainChinese(search)){
                search = URLDecoder.decode(search,"UTF-8");
                byte[] bytes = search.getBytes("ISO-8859-1");
                search = new String(bytes,"UTF-8");
            }
            int page = 1;
            if (null != currentPage) {
                page = Integer.parseInt(currentPage);
            }
            String userId = request.getSession().getAttribute("userId").toString();
            List<Map<String, Object>> list = userService.findList(Integer.parseInt(userId), page, search);
            request.setAttribute("userList", list);
            long totalPage = PageUtil.getTotalPage(userService.findTotalUserCount(search));
            long startPage = PageUtil.getStartPage(page) + 1;
            long endPage = PageUtil.getEndPage(page, totalPage);
            request.setAttribute("totalPage", totalPage);
            request.setAttribute("currentPage", page);
            request.setAttribute("startPage", startPage);
            request.setAttribute("endPage", endPage);
            request.setAttribute("search", search);
            request.getRequestDispatcher("/html/user.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void userDetail(HttpServletRequest request,HttpServletResponse response){
        try {
            String uId = request.getParameter("uId");
            Map<String,Object> detailInfo = userService.getDetail(Integer.parseInt(uId));
            request.setAttribute("detailInfo",detailInfo);
            request.getRequestDispatcher("/html/user_detail.jsp").forward(request,response);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void userLook(HttpServletRequest request,HttpServletResponse response){
        try {
            String uId = request.getParameter("uId");
            Map<String,Object> detailInfo = userService.getDetail(Integer.parseInt(uId));
            request.setAttribute("detailInfo",detailInfo);
            //个人中心因为可以编辑用户信息，有个所有部门信息查询
            List<Dept> deptList = deptService.findAll();
            request.setAttribute("deptList",deptList);
            request.getRequestDispatcher("/html/user_look.jsp").forward(request,response);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void articleList(HttpServletRequest request, HttpServletResponse response) {
        try {
            String currentPage = request.getParameter("page");
            String search = request.getParameter("search");
            if (null != search && !StringUtil.isContainChinese(search)) {
                search = URLDecoder.decode(search, "UTF-8");
                byte[] bytes = search.getBytes("ISO-8859-1");
                search = new String(bytes, "UTF-8");
            }
            int page = 1;
            if (null != currentPage) {
                page = Integer.parseInt(currentPage);
            }

            List<Article> list = articleService.findList();
            request.setAttribute("articleList", list);
            long totalPage = PageUtil.getTotalPage(articleService.findTotalCount(search));
            request.setAttribute("totalPage", totalPage);
            request.setAttribute("currentPage", page);


            long startPage = PageUtil.getStartPage(page) + 1;
            long endPage = PageUtil.getEndPage(page, totalPage);
            request.setAttribute("startPage", startPage);
            request.setAttribute("endPage", endPage);
            request.setAttribute("search", search);
            request.getRequestDispatcher("/html/article.jsp").forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void deptList(HttpServletRequest request, HttpServletResponse response){
        try {
            request.setAttribute("deptList",deptService.findDeptList());
            request.getRequestDispatcher("/html/department.jsp").forward(request, response);
        } catch (Exception e){
            e.printStackTrace();
        }

    }

}


























